<template>
	
	<view>
		<view class="wrap">
			<view class="container">
				<u-sticky>
					<view style="background-color: #FFFFFF;">
						<u-search placeholder="输入商品关键字搜索" v-model="keyword" @custom="toSearch" @search="toSearch"></u-search>
					</view>
				</u-sticky>
			</view>
			<view class="page-box">
				<view v-if="orderList" class="order" v-for="(res, index) in orderList" :key="res.id"@tap='jump(`pages/order/orderDetail`,res.id)'>
					<view class="top">
						<view class="left">
							<u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
							<view class="store">{{ res.createdTime | formatDate }}</view>
							<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
						</view>
						<view v-show="res.status == 0" class="right">交易关闭</view>
						<view v-show="res.status == 1" class="right">等待支付</view>
						<view v-show="res.status == 2" class="right">等待发货</view>
						<view v-show="res.status == 3" class="right">等待收货</view>
						<view v-show="res.status == 4" class="right">等待评论</view>
						<view v-show="res.status == 5" class="right">交易完成</view>
					</view>
					<view class="item" v-for="(item, inIndex) in res.details" :key="inIndex">
						<view class="left"><image :src="vuex_user.store + item.book.img" mode="aspectFill"></image></view>
						<view class="content">
							<view class="title u-line-2">{{ item.book.name }}</view>
							<view class="type">{{ item.book.author }}</view>
							<!-- <view class="delivery-time">发货时间 {{ item.deliveryTime }}</view> -->
						</view>
						<view class="right">
							<view class="price">
								￥{{ item.price | rounding }}
								
							</view>
							<view class="number">x{{ item.count }}</view>
							<view class="evaluate " v-show="res.status == 4">
								<u-button type="warning" size="mini" @click="toEvaluate(res.id,item.id)">去评论</u-button>
							</view>
						</view>
						
					</view>
					<view class="total">
						共{{ res.details.length }}件商品 合计:
						<text class="total-price">
							￥{{ res.price | rounding }}
							
						</text>
					</view>
					<view class="bottom">
						<view class="more"></view>
						<view class="evaluate " v-show="res.status == 0 || res.status == 4">
							<u-button type="error" size="mini" @click="delOrder(res.id)">删除订单</u-button>
						</view>
						<view class="evaluate " v-show="res.status == 1">
							<u-button class="u-m-r-20" type="error" size="mini" @click="cancleOrder(res.id)">取消订单</u-button>
							<u-button type="warning" size="mini" @click='jump(`pages/order/orderDetail`,res.id)'>去支付</u-button>
						</view>
						<view class="evaluate " v-show="res.status == 2 ">
							<u-button type="primary" size="mini" @click='jump(`pages/order/orderDetail`,res.id)'>查看详情</u-button>
						</view>
						<view class="evaluate " v-show="res.status==3">
							<u-button type="primary" size="mini" @click="confirmReceipt(res.id)">确认收货</u-button>
						</view>
						<view class="evaluate " v-show="res.status==5">
							<u-button type="primary" size="mini" @click="delOrder(res.id)">删除订单</u-button>
						</view>

					</view>
				</view>
				<!-- 如果没有数据就显示空 -->
				<view v-if="orderList.length==0">
					<view class="centre">
						<image src="https://cdn.uviewui.com/uview/template/taobao-order.png" mode=""></image>
						<view class="explain">
							您还没有相关的订单
							<view class="tips">可以去看看有那些想买的</view>
						</view>
						<view class="btn" @click="btnToIndex">随便逛逛</view>
					</view>
				</view>
				<u-loadmore v-show="orderList.length>0" class="u-m-b-20" :status="loadStatus" bgColor="#f2f2f2"></u-loadmore>
			</view>
			<u-modal v-model="delModal" content="确定删除订单吗" title="删除" @confirm="delConfirm" :show-cancel-button="true"></u-modal>
			<u-modal v-model="cancleModal" content="确定取消订单吗" title="取消订单" @confirm="cancleConfirm" :show-cancel-button="true"></u-modal>
			<u-modal v-model="confirmModal" content="为了保证您的收货权益,请收到商品确认无误后在确认收货" title="确认收到货了吗" @confirm="confirmConfirm" :show-cancel-button="true"></u-modal>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			orderList: [], 
			keyword:'',
			orderPageNum:1,
			orderTotalPage:0,

			loadStatus: 'loading',
			delModal:false,
			confirmModal:false,
			cancleModal:false,
			operateId:0,
			
		};
	},
	onLoad(val) {
		this.keyword = val.keyword
		this.getAllOrderList()

	},
	onReachBottom() {
		// 判断 如果页数小于总页数  就去获取数据
		if(this.orderPageNum+1 <= this.orderTotalPage){
			
			this.orderPageNum = this.orderPageNum + 1
			this.getAllOrderList()
			
			
		}else{
			
			//  否则将状态改成 nomore
			this.loadStatus = 'nomore'
		}
	},
	methods: {
		toSearch(){
			if(!this.$u.test.isEmpty(this.keyword)){
				this.clear()
				this.getAllOrderList()
			}
			
		},
		//删除订单弹窗确认按钮事件
		async cancleConfirm(){
			const res = await this.$u.api.cancleOrder(this.operateId)
			if(res==null){
				this.clear()
				this.getAllOrderList()
			}
			
		},
		//取消订单
		cancleOrder(val){
			this.cancleModal = true
			this.operateId = val
		},
		
		//删除订单弹窗确认按钮事件
		async delConfirm(){
			const res = await this.$u.api.delOrder(this.operateId)
			
			if(res==null){
				this.clear()
				this.getAllOrderList()
			}
			
		},
		//删除订单
		delOrder(val){
			this.delModal = true
			this.operateId = val

		},
		//去评价
		toEvaluate(val,val2){
			// console.log(val,val2)
			this.$u.route({
				url:'pages/order/orderEvaluate',
				params:{
					oid:val,
					did:val2
				}
			})
		},
		//订单确认收货 确认按钮
		async confirmConfirm(){
			const res = await this.$u.api.confirmReceipt(this.operateId)
			if(res==null){
				this.$u.toast("确认收货成功！快去评价商品吧")
				this.clear()
				this.getAllOrderList()
				
				this.$u.route({
					url:'pages/order/orderEvaluate',
					params:{
						oid:this.operateId
					}
				})
			}
		},
		
		//订单确认收货
		confirmReceipt(val){
			this.confirmModal = true
			this.operateId = val
		},
		
		// 订单详情跳转
		jump(url, id) {
			this.$u.route({
				url,
				params: {
					oid:id
				}
			})
		},
		//跳转首页
		btnToIndex(){
			this.$u.route({
				type:'tab',
				url:'pages/index/index'
			})
		},
		
		//获取全部订单并分类
		async getAllOrderList(){
			const params = {
				pageNum:this.orderPageNum,
				keyword:this.keyword
			}
			const res = await this.$u.api.getOrderList(params)
			console.log(res)
			this.orderList.push(...res.list)
			if(res.totalPage<=1 || res.list.length<10){

				this.loadStatus='nomore'
			}else{
				this.loadStatus='loading'
				this.orderTotalPage = res.totalPage
			}
		},

		clear(){
			this.orderList = []
			this.orderPageNum = 1
			this.orderTotalPage = 0
		}
	}
};
</script>

<style>
/* #ifndef H5 */
page {
	height: 100%;
	background-color: #f2f2f2;
}
/* #endif */
</style>

<style lang="scss" scoped>
.order {
	width: 710rpx;
	background-color: #ffffff;
	margin: 20rpx auto;
	border-radius: 20rpx;
	box-sizing: border-box;
	padding: 20rpx;
	font-size: 28rpx;
	.top {
		display: flex;
		justify-content: space-between;
		.left {
			display: flex;
			align-items: center;
			.store {
				margin: 0 10rpx;
				font-size: 32rpx;
				font-weight: bold;
			}
		}
		.right {
			color: $u-type-warning-dark;
		}
	}
	.item {
		display: flex;
		margin: 20rpx 0 0;
		.left {
			margin-right: 20rpx;
			image {
				width: 200rpx;
				height: 200rpx;
				border-radius: 10rpx;
			}
		}
		.content {
			.title {
				font-size: 28rpx;
				line-height: 50rpx;
			}
			.type {
				margin: 10rpx 0;
				font-size: 24rpx;
				color: $u-tips-color;
			}
			.delivery-time {
				color: #e5d001;
				font-size: 24rpx;
			}
		}
		.right {
			margin-left: 10rpx;
			padding-top: 20rpx;
			text-align: right;
			.decimal {
				font-size: 24rpx;
				margin-top: 4rpx;
			}
			.number {
				color: $u-tips-color;
				font-size: 24rpx;
			}
		}
	}
	.total {
		margin-top: 20rpx;
		text-align: right;
		font-size: 24rpx;
		.total-price {
			font-size: 32rpx;
		}
	}
	.bottom {
		display: flex;
		margin-top: 40rpx;
		padding: 0 10rpx;
		justify-content: space-between;
		align-items: center;
		.btn {
			line-height: 52rpx;
			width: 160rpx;
			border-radius: 26rpx;
			border: 2rpx solid $u-border-color;
			font-size: 26rpx;
			text-align: center;
			color: $u-type-info-dark;
		}
		.evaluate {
			color: $u-type-warning-dark;
			border-color: $u-type-warning-dark;
		}
	}
}
.centre {
	text-align: center;
	margin: 200rpx auto;
	font-size: 32rpx;
	image {
		width: 164rpx;
		height: 164rpx;
		border-radius: 50%;
		margin-bottom: 20rpx;
	}
	.tips {
		font-size: 24rpx;
		color: #999999;
		margin-top: 20rpx;
	}
	.btn {
		margin: 80rpx auto;
		width: 200rpx;
		border-radius: 32rpx;
		line-height: 64rpx;
		color: #ffffff;
		font-size: 26rpx;
		background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
	}
}
.wrap {
	display: flex;
	flex-direction: column;
	height: calc(100vh - var(--window-top));
	width: 100%;
}
.swiper-box {
	flex: 1;
}
.swiper-item {
	height: 100%;
}
	.container {
		
		margin-top: 0rpx;
	}
	
	.sticky {
		width: 750rpx;
		height: 120rpx;
		background-color: #2979ff;
		color: #fff;
		padding: 24rpx;
	}
</style>
